package com.tuanzi.loan.web.bean.family;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.family.LoanNewAdvertCreditCard;
import com.tuanzi.loan.business.service.family.LoanNewAdvertCreditCardService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.LoanNewAdvertCreditCardRequest;
import com.tuanzi.loan.vo.response.family.LoanNewAdvertCreditCardResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.LoanNewAdvertCreditCardLazyDataModel;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 13:43 2018/9/5
 * Description:
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanNewAdvertCreditCardListBean extends JSFListingBean<LoanNewAdvertCreditCardRequest, LoanNewAdvertCreditCardResponse>{
    private static final long serialVersionUID = -7500547298051444409L;

    @Autowired
    private LoanNewAdvertCreditCardService loanNewAdvertCreditCardService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    private void init(){
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new LoanNewAdvertCreditCardRequest();
        lazyDataModel = new LoanNewAdvertCreditCardLazyDataModel($);
    }

    public void onRest(){
        initLazyDataModel();
    }

    public String add(){
        return redirect("/views/family/credithousekeeper/credit-card-advert-info.xhtml");
    }

    public String toInfo(Long id){
        return redirect("/views/family/credithousekeeper/credit-card-advert-info.xhtml?id=" + id);
    }

    public void onLine(Long id){
        if(null == id){
            initLazyDataModel();
            return;
        }

        LoanNewAdvertCreditCard one = loanNewAdvertCreditCardService.findOne(id);
        if(Objects.nonNull(one)){
            one.setStatus(1);
            one.setOnlineTime(new Date());
            loanNewAdvertCreditCardService.saveOrUpdate(one);
        }

        stringRedisTemplate.delete(LoanRedisKey.LOAN_NEW_CREDIT_CARD_LIST);
        initLazyDataModel();
        addSuccessfullyMessage();
    }

    public void offLine(Long id){
        if(null == id){
            initLazyDataModel();
            return;
        }

        LoanNewAdvertCreditCard one = loanNewAdvertCreditCardService.findOne(id);
        if(Objects.nonNull(one)){
            one.setStatus(0);
            one.setOfflineTime(new Date());
            loanNewAdvertCreditCardService.saveOrUpdate(one);
        }

        stringRedisTemplate.delete(LoanRedisKey.LOAN_NEW_CREDIT_CARD_LIST);
        initLazyDataModel();
        addSuccessfullyMessage();
    }


    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.NEW_CREDIT_CARD_ADVERT);
    }
}